<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
<title>Gwack Admin</title>
<link href="style.css" rel="stylesheet" type="text/css" />



<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>

<SCRIPT TYPE="text/javascript">
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=500,height=400,scrollbars=yes');
return false;
}
</SCRIPT>


</head>

<?php
include('menu.php');
?>

<div id="content">
	<div id="leftBox">
		<div class="contentBox">
			<div class="contentBoxTop">
			  <h3>GWACK status</h3>
			</div>
			<div class="innerContent">

			<?php
			$logrequest=$_GET['logrequest'];
			$URLname='global';
			$configfile="/etc/opt/gwack/gwack.xml";
			$xml = simplexml_load_file($configfile);


			$lockfile = '/etc/opt/gwack/gwack.lock';
			$lastrundate = $xml->lastrundate;
			$lastrunstart = $xml->lastrunstart;
			$lastrunend = $xml->lastrunend;
			$Mainlogpath = $xml->logpath;

			if (file_exists($lockfile))
			{
			$running = "1";
			}

			if (substr($Mainlogpath, -1) == "/"){
				$logpath = $Mainlogpath;}
			else {
				$logpath = $Mainlogpath.'/';}

			$loglist = shell_exec("ls -l $logpath | grep -v '^d' | awk '{print $9}'");
			$logfiles = preg_split('/[\r\n]+/', $loglist);



//Function is to create log selector later in the page:

function LogFileSelector($formname = '', $options = array(), $logrequest, $URLname)
{
	echo '<select name="'.$formname.'" onchange="MM_jumpMenu(\'parent\',this,0)">';
	foreach ($options as $option => $value) {
		echo '<option value="status.php?logrequest='.$value.'&name='.$URLname.'">'.$value.'</option>';
	}

	echo '</select>';
}


?>

			<table class="sTable">
				<tr>
				<thead>
				<th><span strong >&nbsp</strong></th>
				<th>Currently Running</th>
				<th>Last Full Run</th>
				<th>Start Time</th>
				<th>End Time</th>
				<th>View Log</th></span>
				</tr>
				</thead>
				<tr class="oddRow">
				<td class="firstCol">Main</td>
				<td>
				<?php if ($running == 1){
						echo "<img height='15' src='images/circle_green.png'></style></td>";
						}
				echo "<td>$lastrundate</td>";
				echo "<td>$lastrunstart</td>";
				echo "<td>$lastrunend</td>";
				echo "<td><form name='form' id='form' target='newwin'>";
				$html = LogFileSelector('logrequest', $logfiles, $logrequest, 'global');
				echo "</td></tr></table>";
				?>
				</form>

				<hr />
<p>
			  <table class="sTable">
                   <tr>
                   <thead>
                   <th><span strong >&nbsp</strong></th>
                   <th>Currently Running</th>
                   <th>Restore Path</th>
                   <th>Start Time</th>
                   <th>End Time</th>
                   <th>View Log</th></span>
                    </tr>
                    </thead>
                    <tr class="oddRow">

				<?php
				 foreach ($xml->server as $server)
				{
					$URLname = $server->name;
					$name = $server->name;
					$remotepath = $server->remotepath;
					$type = $server->type;
					$currentfulldate = $server->currentfulldate;
					$currentfullpath = $server->currentfullpath;
					$restorepath = $server->restorepath;
					$starttime = $server->starttime;
					$endtime = $server->endtime;
					$xmllogpath = $xml->logpath;
					$restore = basename($restorepath);

					if (substr($xmllogpath, -1) == "/"){
						$logpath = $xmllogpath.$name.'/';}
					else {
						$logpath = $xmllogpath.'/'.$name.'/';}
	
					$lockfile = '/etc/opt/gwack/gwack.lock';

					if (file_exists($lockfile))
					{
						$islock = shell_exec('grep '.$name.' '.$lockfile.'');
						if (isset($islock))
						{	
							$running = 1;
						}
					}
					
					$loglist = shell_exec("ls -l $logpath | grep -v '^d' | awk '{print $9}'");
        			$logfiles = preg_split('/[\r\n]+/', $loglist);

					echo "<td class='firstCol'>$name</td>";
                    echo "<td>";




    	            if ($running == 1){
                        echo "<img height='15' src='images/circle_green.png'></style></td>";
                        }
    	            echo "<td>$restore</td>";
    	            echo "<td>$starttime</td>";
    	            echo "<td>$endtime</td>";
    	            echo "<td><form name='objform' id='objform'>";
    	            $html = LogFileSelector('logrequest', $logfiles, $logrequest, $name);
    				echo "</form></tr>";	            
				}

				?>
</form>
</table>

		



			
			<label for="logView"></label>
			<textarea name="logView" id="logView" cols="72" rows="9"><?php $LogFile = shell_exec('cat '$logpath.$logrequest.''); echo $LogFile; ?></textarea>


    </div><!-- end innerContent -->
        </div><!-- end contentBox -->

 
  </div><!-- end leftBox -->




</div><!-- end content -->
